package game;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;

import system.*;

public class Title extends Scene{

	AnimatedSprite background, background2, planet, title, menusprite, nameentry, underscore;
	TextHandler nameHandler;
	Actor bg1a, bg1b, menu;
	Gigatron game;

	boolean nameEntryMode;

	final int nameEntryX = 100;
	final int nameEntryY = 100;

	int charSlot = 0;
	int fadeDelay = 0;
	int alphaIndex = 0;
	
	String input = "";

	public Title(Game aGame, InputManager IM, SoundLibrary jukebox) {
		super(aGame, IM, jukebox);
		game = (Gigatron)aGame;
		nameEntryMode = false;

		nameHandler = new TextHandler(nameEntryX+120, nameEntryY+6, "score", -1, 0, true);

		try {
			background = new AnimatedSprite(new File("assets/sprites/title-stars.png"), 480, 270, false, 0);
			background2 = new AnimatedSprite(new File("assets/sprites/title-stars.png"), 480, 270, false, 0);
			planet = new AnimatedSprite(new File("assets/sprites/title-planet.png"), 480, 270, false, 0);
			title = new AnimatedSprite(new File("assets/sprites/title-text.png"), 480, 270, false, 0);
			menusprite = new AnimatedSprite(new File("assets/sprites/title-menu.png"), 219, 67, false, 0);
			nameentry = new AnimatedSprite(new File("assets/sprites/title-nameentry.png"), 199, 24, false, 0);
			underscore = new AnimatedSprite(new File("assets/sprites/title-charunderline.png"), 8, 3, false, 0);

			bg1a = new Actor("Parallax", background, 0, 0, -1, 0, 0);
			bg1b = new Actor("Parallax", background2, 480, 0, -1, 0, 0);

			menu = new Actor("menu", menusprite, 45, 145, 0, 0, 0);

		} catch (IOException e) {
			System.out.println("Could not find title screen");
		}
		
		jukebox.Play("intro.wav");

	}

	@Override
	public void paint(Graphics g){

		Graphics2D g2d = (Graphics2D)g;

		g2d.setColor(new Color(2,3,12));
		g2d.fillRect(0, 0, 480, 270);

		g2d.drawImage(bg1a.getImage(),bg1a.getX(),bg1a.getY(), getGame());
		g2d.drawImage(bg1b.getImage(),bg1b.getX(),bg1b.getY(), getGame());
		g2d.drawImage(planet.getImage(), 0, 0, getGame());

		if(alphaIndex == 49){
			jukebox.Play("bgm-title.wav");
		}
		if(alphaIndex >= 50){
			g2d.drawImage(menu.getImage(), menu.getX(), menu.getY(), getGame());
		}

		if(nameEntryMode){
			g.drawImage(nameentry.getImage(), nameEntryX, nameEntryY, getGame());
			if (charSlot < 10){
				g.drawImage(underscore.getImage(), nameEntryX + 120 + 7*charSlot, nameEntryY+17, getGame());
			} else {
				g.drawImage(underscore.getImage(), nameEntryX + 120 + 7*(charSlot-1), nameEntryY+17, getGame());
			}
			for(int j = 0; j < 10; j++){
				g.drawImage(nameHandler.getTextAt(j).getImage(), nameHandler.getTextAt(j).getX(), nameHandler.getTextAt(j).getY(), game);
			}

		}
		
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaIndex * 0.02f));
		g2d.drawImage(title.getImage(), 0, 0, getGame());

	}

	@Override
	public void update(){
		bg1a.update();
		bg1b.update();
		nameHandler.update();

		if(alphaIndex >= 50 && !nameEntryMode){
			if(inputs.isPressing(inputs.KEY_CONFIRM)){
				jukebox.Play("cursor.wav");
				inputs.handledKey(inputs.KEY_CONFIRM);	
				switch(menu.getState()){
				case 0:
					nameEntryMode = true;
					inputs.handledKey(inputs.KEY_SPACE);
					break;
				case 1:
					game.setState(Game.GAME_HIGHSCORES);
					break;
				case 2:
					System.exit(0);
					break;
				}

			}
			if(inputs.isPressing(inputs.KEY_UP)){
				jukebox.Play("cursor.wav");
				int newState = menu.getState() - 1;
				if(newState < 0){
					newState = 2;
				}
				menu.setState(newState);
				inputs.handledKey(inputs.KEY_UP);
			}
			if(inputs.isPressing(inputs.KEY_DOWN)){
				jukebox.Play("cursor.wav");
				int newState = menu.getState() + 1;
				if(newState > 2){
					newState = 0;
				}
				menu.setState(newState);
				inputs.handledKey(inputs.KEY_DOWN);
			}
			menu.setSprite(menu.getState());
		}
		
		if(nameEntryMode) {

			// NAME ENTRY KEY HANDLERS
			if(inputs.isPressing(inputs.KEY_CANCEL)){
				jukebox.Play("cursor.wav");
				nameEntryMode = false;
				inputs.handledKey(inputs.KEY_CANCEL);
			}
			if(inputs.isPressing(inputs.KEY_BACKSPACE)){
				if (charSlot > 0){
					jukebox.Play("cursor.wav");
					if (charSlot == 1){
						input = new String("");
						charSlot = 0;
					} else {
						charSlot--;
						input = input.substring(0, charSlot);
					}
				}
				inputs.handledKey(inputs.KEY_BACKSPACE);
			}
			if(inputs.isPressing(inputs.KEY_SPACE)){
				jukebox.Play("cursor.wav");
				input = input + " ";
				charSlot++;
				inputs.handledKey(inputs.KEY_SPACE);

			}
			if(inputs.isPressing(inputs.KEY_CONFIRM)){
				jukebox.Play("cursor.wav");

				game.setPlayerName(input.replace(" ", "_"));
				game.setState(Game.GAME_RUN);
				inputs.handledKey(inputs.KEY_CONFIRM);

			}
			
			if(inputs.getKeyTyped() >= 65 && inputs.getKeyTyped() <= 90 && charSlot < 10){
				jukebox.Play("cursor.wav");
				input = input + (char) inputs.getKeyTyped();
				inputs.resetKeyTyped();
				charSlot++;
			}
			
			nameHandler.setText(input);
			nameHandler.requireUpdate(true);
		}

		menu.update();

		if(alphaIndex < 50){
			fadeDelay++;
			if(fadeDelay >= 10){
				alphaIndex++;
			}
		}


	}
	
	// returns true if the scene is ready for BGM to be played
	public boolean isDone(){
		return (alphaIndex>=49);
	}
}


